/* 
 * $Id: pybpf.h 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
 *
 * Copyright (c) 2006, 2007, 2008 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef __PY_BPF_H_
#define __PY_BPF_H_

#include <sys/types.h>
#include <sys/socket.h>

#include <sys/time.h>
#include <sys/ioctl.h>

#include <arpa/inet.h>

#include <net/bpf.h>
#include <net/if.h>
#include <net/if_dl.h>

#include <netinet/in.h>

#include <fcntl.h>
#include <ifaddrs.h>
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <Python.h>
#include <listobject.h>
#include <structmember.h>

#define MAXBUF 4096
#define BPF_MEMBER(object) struct PyMemberDef object
#define BPF_MEMBER_LIST(a,b,c,d,e) {a,b,c,d,e}
#define BPF_ALLOC(object, type) (object *)type.tp_alloc(&type, 0) 
#define BPF_FREE(ob) ob->ob_type->tp_free((PyObject *)ob)

typedef struct {
   PyObject_HEAD
   PyObject *converter;
   int fd;
   char *iface;
} _bpf_object;

static PyObject *_bpfError; 
extern PyTypeObject _bpf_object_type;

const char __bpf[sizeof "/dev/bpf0000000000"];
const char *__bpf_version = "0.1";
static char __bpf_doc[] = "bpf handling using python";

void init_bpf(void);
static void _bpf_object_dealloc(_bpf_object *);
static int _bpf_traverse(_bpf_object *, visitproc, void *);
static int _bpf_setattr(_bpf_object *, char *, PyObject *);
static PyObject *_bpf_getattr(_bpf_object *, char *);
static PyObject *bpf_open(PyObject *, PyObject *);
static PyObject *bpf_close(_bpf_object *, PyObject *);
static PyObject *get_dl(_bpf_object *, PyObject *);
static PyObject *get_dl_list(_bpf_object *, PyObject *);
static PyObject *get_if(_bpf_object *, PyObject *);
static PyObject *set_if(_bpf_object *, PyObject *);
static PyObject *get_stats(_bpf_object *, PyObject *);
static PyObject *set_dl(_bpf_object *, PyObject *);
static PyObject *bpf_flush(_bpf_object *);
static PyObject *set_async(_bpf_object *, PyObject *);
static PyObject *set_filter(_bpf_object *, PyObject *, PyObject *);

static PyMethodDef PyBpfMethods[] = {
   { "start", (PyCFunction)bpf_open, METH_VARARGS, NULL },
   { "stop", (PyCFunction)bpf_close, METH_VARARGS, NULL },
   { "get_dl", (PyCFunction)get_dl, METH_VARARGS, NULL },
   { "get_dl_list", (PyCFunction)get_dl_list, METH_VARARGS, NULL },
   { "get_if", (PyCFunction)get_if, METH_VARARGS, NULL },
   { "set_if", (PyCFunction)set_if, METH_VARARGS, NULL },
   { "get_stats", (PyCFunction)get_stats, METH_VARARGS, NULL },
   { "set_dl", (PyCFunction)set_dl, METH_VARARGS, NULL },
   { "flush", (PyCFunction)bpf_flush, NULL, NULL },
   { "set_async", (PyCFunction)set_async, METH_VARARGS, NULL },
   { "set_filter", (PyCFunction)set_filter, 
      METH_VARARGS | METH_KEYWORDS, NULL },
   { NULL, NULL, 0, NULL },
};

static BPF_MEMBER(_bpf_object_memberlist)[] = {
   BPF_MEMBER_LIST("converter", T_OBJECT, offsetof(_bpf_object, converter), RO, 
         "Type conversion mapping"),
   { NULL, 0, 0, 0, NULL }
};

#endif

